﻿<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%option explicit%>
<%
Dim RZ_IN
RZ_IN=1
%>
<!--#include file="../conn.inc.asp" -->
<!--#include file="admin_inc/admin.function.asp" -->
<!--#include file="admin_inc/admin.sqlfunction.asp" -->
<%
'作者：山林客（ah_bill）
'博客：http://ruizhinet.blog.163.com
'网站：http://www.ruizhinet.cn
'本信息不会影响您网站的正常访问，请保留
checkadmin(4)
dim act,name,fenlei,engname,theorder,visible,url,parent_id,parent_path,parent_level,typeimg,id,strid,rst,sqlt,recordcount,lastid,content_chn,content_eng,pathname_chn,pathname_eng,oldname,oldengname, template,perpage,keyword_chn,keyword_eng,recordtemplate
act=HTMLEncode(request.Form("act"))
name=HTMLEncode(request.Form("name"))
fenlei=CInt(request.Form("fenlei"))
engname=HTMLEncode(request.Form("engname"))
oldname=HTMLEncode(request.Form("oldname"))
oldengname=HTMLEncode(request.Form("oldengname"))
theorder=HTMLEncode(request.Form("theorder"))
perpage=HTMLEncode(request.Form("perpage"))
visible=HTMLEncode(request.Form("visible"))
template=trim(request.Form("template"))
recordtemplate=trim(request.Form("recordtemplate"))
If IsNull(recordtemplate) Then recordtemplate=""
If visible="" Or isempty(visible) Then
	visible=0
End If 
url=trim(request.Form("url"))
parent_id=HTMLEncode(request.Form("parent_id"))
parent_path=HTMLEncode(request.Form("parent_path"))
parent_level=HTMLEncode(request.Form("parent_level"))
content_chn=request.Form("content_chn")
content_eng=request.Form("content_eng")
keyword_chn=HTMLEncode(request.Form("keyword_chn"))
keyword_eng=HTMLEncode(request.Form("keyword_eng"))

If content_chn<>"" Then
	content_chn=Replace(content_chn,"{","&#123;")
End If 
If content_eng<>"" Then
	content_eng=Replace(content_eng,"{","&#123;")
End If 
If perpage="" Or IsEmpty(perpage) Then
	perpage=20
End If 
typeimg=request.Form("typeimg")
id=HTMLEncode(request.Form("id"))
strid=","&id&"," 
If act="addsave" Then
	If (name="") Then
		historygoback("请填写分类名称")
	End If 
	If (theorder="") Then
		historygoback("请填写分类顺序")
	End If 
	Set rst = Server.CreateObject("ADODB.Recordset")
	sqlt = "select * from [c_type]"
	rst.open sqlt,conn,1,3
	rst.addnew
	rst("name")=name
	rst("engname")=engname
	rst("fenlei")=fenlei
	rst("parentid")=parent_id
	rst("path")=parent_path
	rst("url")=url
	If theorder<>"" Then
	rst("theorder")=CInt(theorder)
	End If 
	rst("visible")=visible
	rst("thelevel")=CInt(parent_level)+1
	rst("typeimg")=typeimg
	rst("content_chn")=content_chn
	rst("content_eng")=content_eng
	rst("keyword_chn")=keyword_chn
	rst("keyword_eng")=keyword_eng
	rst("template")=template
	rst("recordtemplate")=recordtemplate
	rst("perpage")=perpage
	rst("makehtmltime")="2000-01-01"
	rst.update
	lastid=rst("id")
	rst.close
	Set rst=Nothing 
	newpath=parent_path+CStr(lastid)+","
	pathname_chn=GeneratePathName(newpath,0)
	pathname_eng=GeneratePathName(newpath,1)
	conn.Execute("update c_type set path='"&newpath&"',pathname_chn='"&pathname_chn&"',pathname_eng='"&pathname_eng&"' where id="&lastid)
	conn.Execute("update c_type set havechild=1 where id="&parent_id)
	Call updateTypeModifytime(CutPath(newpath),Now())
	Call SetTypeProperties()
	If app("rz_static")=1 Then
		response.write("<script type='text/javascript'>var flag;flag=confirm('您对分类进行了修改，您是否要更新相关HTML文件');")
		response.write("if(flag==true){location.href='makehtml.asp?onestep=1';}")
		response.write("else{location.href='showmsg.asp?from=type_list.asp&msg=AddSuccess';}")
		response.write("</script>")
	Else
	response.redirect("showmsg.asp?from=type_list.asp&msg=AddSuccess")
	End If 
End If 

If act="modifysave" Then 
	If (name="") Then
		historygoback("请填写分类名称")
	End If 
	If (theorder="") Then
		historygoback("请填写分类顺序")
	End If 
	Set rst = Server.CreateObject("ADODB.Recordset")
	sqlt = "select * from [c_type] where id="&id
	rst.open sqlt,conn,1,3
	rst("name")=name
	rst("engname")=engname
	rst("fenlei")=fenlei
	rst("url")=url
	If theorder<>"" Then
	rst("theorder")=CInt(theorder)
	End If 
	rst("visible")=visible
	rst("typeimg")=typeimg
	rst("content_chn")=content_chn
	rst("content_eng")=content_eng
	rst("keyword_chn")=keyword_chn
	rst("keyword_eng")=keyword_eng
	rst("template")=template
	rst("recordtemplate")=recordtemplate
	rst("perpage")=perpage
	newpath=rst("path")
	rst.update
	rst.close
	Set rst = Server.CreateObject("ADODB.Recordset")
	sqlt = "select pathname_chn,pathname_eng from c_type where instr(pathname_chn,'"&oldname&"')>0 or instr(pathname_eng,'"&oldengname&"')>0"
	rst.open sqlt,conn,1,3
	do while not rst.eof
		rst("pathname_chn")=ChangePathName(rst("pathname_chn"),oldname,name)
		rst("pathname_eng")=ChangePathName(rst("pathname_eng"),oldengname,engname)
	rst.update
	rst.movenext
	loop
	rst.close
	set rst=Nothing
	Call updateTypeModifytime(CutPath(newpath),Now())
	Call SetTypeProperties()
	If app("rz_static")=1 Then
		response.write("<script type='text/javascript'>var flag;flag=confirm('您对分类进行了修改，您是否要更新相关HTML文件');")
		response.write("if(flag==true){location.href='makehtml.asp?onestep=1';}")
		response.write("else{location.href='showmsg.asp?from=type_list.asp&msg=ModifySuccess';}")
		response.write("</script>")
	Else
	response.redirect("showmsg.asp?from=type_list.asp&msg=ModifySuccess")
	End If 
End If 

If act="movesave" Then
	Dim frompath,fromid,fromparentid,upper2,toid,topath,newlevel,newpath,rsp,sqlp,upper
	frompath=HTMLEncode(request.Form("frompath"))
	fromid=CStr(HTMLEncode(request.Form("fromid")))
	fromparentid=CStr(HTMLEncode(request.Form("fromparentid")))
	upper=HTMLEncode(request.Form("upper"))
	upper2=Split(upper,"_")
	toid=upper2(0)
	topath=upper2(1)
	newlevel=CStr(CInt(upper2(2))+1)
	newpath=topath+CStr(fromid)+","
	Dim len_frompath
	len_frompath=CStr(Len(frompath)+1)
	Set rsp = Server.CreateObject("ADODB.Recordset")
	sqlp = "select count(id) as recordcount from [c_type] where parentid="&fromparentid&""
	rsp.open sqlp,conn,1,1
	If Not rsp.eof Then
	recordcount=rsp("recordcount")
	End If 
	rsp.close
	Set rsp=Nothing
	conn.BeginTrans
	If recordcount=1 Then 
		conn.execute("update [c_type] set havechild=0 where id="&fromparentid)
		if conn.Errors.Count>0 Then
			conn.Errors.Clear
			conn.RollBackTrans
			response.redirect("showmsg.asp?from=type_list.asp&msg=DatabaseError")
		end if
	End If
	conn.Execute("update c_type set thelevel="+newlevel+",  path='"+newpath+"'+Mid(path,"+len_frompath+") where Instr(path,'"+frompath+"')>0")
	if conn.Errors.Count>0 Then
		conn.Errors.Clear
		conn.RollBackTrans
		response.redirect("showmsg.asp?from=type_list.asp&msg=DatabaseError")
	end If
	conn.Execute("update c_type set havechild=1 where id="+toid)
	if conn.Errors.Count>0 Then
		conn.Errors.Clear
		conn.RollBackTrans
		response.redirect("showmsg.asp?from=type_list.asp&msg=DatabaseError")
	end if
	conn.Execute("update c_type set parentid="+toid+" where id="+fromid)
	if conn.Errors.Count>0 Then
		conn.Errors.Clear
		conn.RollBackTrans
		response.redirect("showmsg.asp?from=type_list.asp&msg=DatabaseError")
	end if
	select case fenlei
		case 1:
			conn.Execute("update c_article set path='"+newpath+"'+Mid(path,"+len_frompath+") where Instr(path,'"+frompath+"')>0")
		case 2:
			conn.Execute("update c_product set path='"+newpath+"'+Mid(path,"+len_frompath+") where Instr(path,'"+frompath+"')>0")
		case 3:
			conn.Execute("update c_photo set path='"+newpath+"'+Mid(path,"+len_frompath+") where Instr(path,'"+frompath+"')>0")
	end select
	if conn.Errors.Count>0 Then
		conn.Errors.Clear
		conn.RollBackTrans
		response.redirect("showmsg.asp?from=type_list.asp&msg=DatabaseError")
	end If
	conn.CommitTrans
	GeneratePathNames
	Call updateTypeModifytime(CutPath(newpath),Now())
	Call SetTypeProperties()
	If app("rz_static")=1 Then
		response.write("<script type='text/javascript'>var flag;flag=confirm('您对分类进行了修改，您是否要更新相关HTML文件');")
		response.write("if(flag==true){location.href='makehtml.asp?onestep=1';}")
		response.write("else{location.href='showmsg.asp?from=type_list.asp&msg=MoveSuccess';}")
		response.write("</script>")
	Else
	response.redirect("showmsg.asp?from=type_list.asp&msg=MoveSuccess")
	End If 
End If 

If act="unionsave" Then
	dim pos
	upper=HTMLEncode(request.Form("upper"))
	frompath=HTMLEncode(request.Form("frompath"))
	pos=Instr(upper, "_")
	toid=CStr(mid(upper,1,pos-1))
	topath=mid(upper,pos+1)
	newpath=topath
	conn.BeginTrans
	select case fenlei
		case 1:
			conn.Execute("update c_article set path='"+newpath+"',typeid="+toid+" where Instr(path,'"+frompath+"')>0")
		case 2:
			conn.Execute("update c_product set path='"+newpath+"',typeid="+toid+" where Instr(path,'"+frompath+"')>0")
		case 3:
			conn.Execute("update c_photo set path='"+newpath+"',typeid="+toid+" where Instr(path,'"+frompath+"')>0")
	end select
	if conn.Errors.Count>0 Then
		conn.Errors.Clear
		conn.RollBackTrans
		response.redirect("showmsg.asp?from=type_list.asp&msg=DatabaseError")
	end If
	conn.Execute("delete from c_type where Instr(path,'"+frompath+"')>0")
	if conn.Errors.Count>0 Then
		conn.Errors.Clear
		conn.RollBackTrans
		response.redirect("showmsg.asp?from=type_list.asp&msg=DatabaseError")
	end If
	Set rsp = Server.CreateObject("ADODB.Recordset")
	sqlp = "select count(id) as recordcount from [c_type] where parentid="&toid&""
	rsp.open sqlp,conn,1,1
	If Not rsp.eof Then
	recordcount=rsp("recordcount")
	End If 
	rsp.close
	Set rsp=Nothing
	If recordcount=0 Then 
		conn.execute("update [c_type] set havechild=0 where id="&toid)
		if conn.Errors.Count>0 Then
			conn.Errors.Clear
			conn.RollBackTrans
			response.redirect("showmsg.asp?from=type_list.asp&msg=DatabaseError")
		end If
	End If	
	conn.CommitTrans
	Call updateTypeModifytime(CutPath(newpath),Now())
	Call SetTypeProperties()
	If app("rz_static")=1 Then
		response.write("<script type='text/javascript'>var flag;flag=confirm('您对分类进行了修改，您是否要更新相关HTML文件');")
		response.write("if(flag==true){location.href='makehtml.asp?onestep=1';}")
		response.write("else{location.href='showmsg.asp?from=type_list.asp&msg=UnionSuccess';}")
		response.write("</script>")
	Else
	response.redirect("showmsg.asp?from=type_list.asp&msg=UnionSuccess")
	End If 
End If 

If act="updatepathname" Then 
	GeneratePathNames
	response.redirect("showmsg.asp?from=type_list.asp&msg=RefreshSuccess")
End If 

Function GeneratePathNames
	Set rst = Server.CreateObject("ADODB.Recordset")
	sqlt = "select * from c_type"
	rst.open sqlt,conn,1,3
	do while not rst.eof
		rst("pathname_chn")=GeneratePathName(rst("path"),0)
		rst("pathname_eng")=GeneratePathName(rst("path"),1)
		Dim dotnums 
		dotnums=Split(rst("path"),",")
		rst("thelevel")=UBound(dotnums)
	rst.update
	rst.movenext
	loop
	rst.close
	set rst=Nothing
End Function 

Function GeneratePathName(path,g_lan)
	Dim g_path
	g_path=Left(path,(Len(path)-1))
	Dim path_arr,i,id,rs,sql,result
	path_arr= Split(g_path,",")
	For i=0 To UBound(path_arr)
		id=path_arr(i)
		Set rs = Server.CreateObject("ADODB.Recordset")
		sql = "select name,engname from [c_type] where id="&id
		rs.open sql,conn,1,1
		If Not rs.eof Then
			If g_lan=0 Then
				If rs("name")="" Then
				result=result+"&nbsp;"+","
				else
				result=result+rs("name")+","
				End if
			Else
				If rs("engname")="" Then
				result=result+"&nbsp;"+","
				else
				result=result+rs("engname")+","
				End if
			End If 
		End If
		rs.close
		Set rs=Nothing
	Next 
	GeneratePathName=result
End Function

Function ChangePathName(pathname,oldname,newname) 
	if pathname<>"" and (not isnull(pathname)) then
		ChangePathName=replace(pathname,oldname,newname)
	else
		ChangePathName=pathname
	end if
End Function
%>